from selenium import webdriver
from selenium.webdriver.common import action_chains
import time
import op1test
import requests
from requests.cookies import RequestsCookieJar

driver = webdriver.Chrome()
driver.get('https://www.sf-express.com/cn/sc/dynamic_function/waybill/#search/bill-number/')  # 快递查询的URL
driver.maximize_window()
driver.find_element_by_xpath('//input[@class="token-input"]').send_keys('XXXXXX')  # 输入顺丰快递单号
driver.find_element_by_xpath('//*[@id="queryBill"]').click()
time.sleep(1.5)
driver.switch_to.frame("tcaptcha_popup")           # 这里验证窗口需要且到它的frame
ttt1 = driver.find_element_by_xpath('//*[@id="slideBkg"]').get_attribute('src')    # 获取验证图片的URL
print(ttt1)
ttt2 = driver.find_element_by_xpath('//*[@id="slideBlock"]').get_attribute('src')  # 获取切割出的滑块图片的URL
co = driver.get_cookies()   # 获取cookie对象          # 保存一下当前浏览器的cookie

# 认证获取图片
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
jar = RequestsCookieJar()
for cookie in co:
    jar.set(cookie['name'], cookie['value'])   # 存储cookie jar，下面给requests请求
s = requests.Session()
r1 = s.get(url=ttt1, headers=headers, cookies=jar)
r2 = s.get(url=ttt2)
with open('template.jpg', 'wb') as f_obj:   # 下载认证图片
    f_obj.write(r1.content)

with open('target.jpg', 'wb') as f_obj:     # 下载拼接的图片
    f_obj.write(r2.content)
lent = op1test.return_postion() - 80        # 减去滑块本身和缝隙的宽度
drag_box = driver.find_element_by_xpath('//*[@id="tcaptcha_drag_thumb"]')
actions = action_chains.ActionChains(driver)
actions.click_and_hold(drag_box).perform()
# actions.reset_actions()

L = [0, lent/12, lent/6, lent/4, 0, lent/4, lent/6, lent/12, 0, 0]       # 模仿手动的时候，速度0-加速-减速-0
for i in L:
    actions.move_by_offset(i, 1).perform()
    time.sleep(0.1)
    actions = action_chains.ActionChains(driver)      # 重置动作链，免得步进是累加的
actions.reset_actions()   # 松开滑块






